$.swiper = function (set){
    var defaults ={
        interval:3000,//轮播时间
        // width:200,//宽
        // height:160,//高
        //len:5
        content:'#pageView',
        autoplay:false

    }
    var set = $.extend(defaults,set||{})

    var container = $('.layer-slider');
    var list = $(set.content).find('.layer-slider .list');
    //list.prepend(list.children("img:last-child").clone());
    if(!$("#prev")[0]){
        list.after('<a href="javascript:;" id="prev" style="display:none;">&lt;</a>')
    }
    // var buttons = $('#buttons span');
    var prev = $('#prev');
    var index = 1;
    var len = list.find("img").length-1;
    var lenNum = (len+1)*100 +'%';
    var interval = set.interval;
    var animated = false;//轮播状态
    var timer;
    // $('.layer-slider').width(set.width).height(set.height);
    // list.css({"width":lenNum,"height":"100%","left":'-100%'});
    // list.find("img").css("width",(100/(len+1))+"%");
    function animate (offset) {
        if (parseInt(offset)>0) {
            offset = '+=' + offset;
        } else {
            offset = '-=' + Math.abs(offset.replace("%",""))+"%";
        }
        var left = parseInt(list.css('left')) ;
        list.animate({'left': offset}, 500, function () {
            if(parseInt(list.css('left')) > -10){
                list.css('left', -100 * (len)+"%");
            }
            // }else if(left < -100*(len)+"%") {



            //     list.css('left', -100+"%");
            // }
        });
    }



    function play() {
        animated =true;
        timer = setTimeout(function () {
            prev.trigger('click');
            play()
        }, interval);
    }
    function stop() {
        animated =false;
        clearTimeout(timer);
    }
    function animateTo(nowIndex){
        animate((index-nowIndex)*100+"%")
        index =nowIndex
    }
    // next.bind('click', function () {
    //     if (list.is(':animated')) {
    //         return;
    //     }
    //     if (index == 5) {
    //         index = 1;
    //     }
    //     else {
    //         index += 1;
    //     }
    //     animate(-set.width);
    //     //showButton();
    // });

    prev.bind('click', function () {
        if (index == 1) {
            index = len;
        }
        else {
            index -= 1;
        }
        animate('100%');
        //showButton();
    });

    container.mouseover(function(){
         if(animated){
            stop()
            animated =true;
         }
        
    }).mouseout(function(){
         if(animated){
            play();
         }
    })
    //container.hover(stop, play);

    //play();
    if(set.autoplay){
        animated =true;
        play()
    }
    window.play =play;
    window.stop= stop;
    window.animateTo =animateTo;
    // return {
    //     play:play,
    //     stop:stop
    // }
};